﻿(function ($) {
    // 当domReady的时候开始初始化
    $(function () {

        alert('1231312');

        var $wrap = $('#uploader'),

            // 文件容器
            $queue = $('<ul class="filelist"></ul>')
                .appendTo($wrap.find('.queueList')),

            // 状态栏，包括进度和控制按钮
            $statusBar = $wrap.find('.statusBar'),

            // 文件总体选择信息。
            $info = $statusBar.find('.info'),

            // 状态信息。
            $tips = $statusBar.find('.tips'),

            // 上传按钮
            $upload = $wrap.find('.uploadBtn'),

            // 没选择文件之前的内容。
            $placeHolder = $wrap.find('.placeholder'),

            $progress = $statusBar.find('.progress').hide(),

            // 添加的文件数量
            fileCount = 0,

            // 添加的文件总大小
            fileSize = 0,

            // 优化retina, 在retina下这个值是2
            ratio = window.devicePixelRatio || 1,

            // 缩略图大小
            thumbnailWidth = 110 * ratio,
            thumbnailHeight = 110 * ratio,

            // 可能有pedding, ready, uploading, confirm, done.
            state = 'pedding',

            // 所有文件的进度信息，key为file id
            percentages = {},

            supportTransition = (function () {
                var s = document.createElement('p').style,
                    r = 'transition' in s ||
                        'WebkitTransition' in s ||
                        'MozTransition' in s ||
                        'msTransition' in s ||
                        'OTransition' in s;
                s = null;
                return r;
            })(),

            // WebUploader实例
            uploader;

        if (!WebUploader.Uploader.support()) {
            alert('Web Uploader 不支持您的浏览器！如果你使用的是IE浏览器，请尝试升级 flash 播放器');
            throw new Error('WebUploader does not support the browser you are using.');
        } else {
            alert('您的浏览器可以使用WebUploader进行上传文件');
        }

        // 实例化
        var defaults = {
            pick: {
                id: '#filePicker',
                label: '点击选择文件hahah'
            },
            dnd: '#uploader .queueList',
            paste: document.body,

            //accept: {
            //	title: 'Images',
            //	extensions: 'gif,jpg,jpeg,bmp,png',
            //	mimeTypes: 'image/*'
            //},

            // swf文件路径
            swf: '~/webuploader/Uploader.swf',
            auto: true,
            disableGlobalDnd: true,
            //runtimeOrder :'flash',
            chunked: true,
            chunkSize: 1000 * 1024,
            //server: '/document/fileupload',
            fileNumLimit: 300,
            fileSizeLimit: 10 * 1024 * 1024 * 1024,    // 10G
            fileSingleSizeLimit: 10 * 1024 * 1024 * 1024,    // 10G
            compress: false,
            formData: { guid: generateUUID(), code: "", secret: "", upinfo: "" }
        }
        var option = {};
        var optionwebup = {};

        window.addEventListener("message", function (e) {
            if (e.data != null) {
                option = JSON.parse(e.data);
                if (option.webupconfig) {
                    optionwebup = $.extend(defaults, JSON.parse(option.webupconfig));
                    optionwebup.formData.code = option.usercode;
                    optionwebup.formData.secret = option.secret;
                    optionwebup.formData.upinfo = option.upinfo;


                }
                alert('我要初始化控件了啊！！');
                uploader = WebUploader.create(optionwebup);
                initupload();
            }
        });

        function generateUUID() {
            var d = new Date().getTime();
            var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                var r = (d + Math.random() * 16) % 16 | 0;
                d = Math.floor(d / 16);
                return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
            });
            return uuid;
        };
    });

    //分片
    //https://github.com/fex-team/webuploader/issues/142
    //https://github.com/fex-team/webuploader/issues/139

})(jQuery);